﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;

namespace WinForm.Controls
{
    public partial class ucGridControl : UserControl
    {
        public ucGridControl()
        {
            InitializeComponent();
        }

        public GridControl GridControl
        {
            get { return gridControl1; }
        }

        /// <summary>
        /// GridControl的DataSource
        /// </summary>
        public object DataSource
        {
            get { return gridControl1.DataSource; }
            set { gridControl1.DataSource = value; }
        }

        /// <summary>
        /// GridControl的DataSource
        /// </summary>
        public GridView gridView1
        {
            get { return gridView11; }
        }

        private void gridControl1_DataSourceChanged(object sender, EventArgs e)
        {
            InitGridViewAppearence();
        }
        

        // 显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        
        /// <summary>
        /// 设置GridView的样式
        /// </summary>
        private void InitGridViewAppearence()
        {
            foreach (GridColumn column in gridView11.Columns)
            {
                //ID 列不显示
                if (column.Name.Contains("Id") || column.Name.Contains("ID"))
                    column.Visible = false;
                //所有列不能获得焦点
                column.OptionsColumn.AllowFocus = false;
            }
        }

        private void gridView11_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e)
        {
            if (gridView1.RowCount != 0) return;

            string str = "没有查询到你所想要的数据!";
            Font f = new Font("宋体", 10, FontStyle.Bold);
            Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5);
            e.Graphics.DrawString(str, f, Brushes.Black, r);
        }
    }
}
